package com.jf.utils;

import com.jf.hb.HB;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-16
 * Time:1:31
 */
public class CardSet {
    private final ArrayList<Card> cards = new ArrayList<Card>();
    private final int n;
    private final boolean isFirst;

    private static final int[] binary = new int[6];
    private static int index = 5;

    public CardSet(int n) {
        this(n, false);
    }

    public CardSet(int n, boolean isFirst) {
        this.n = n;
        this.isFirst = isFirst;
        init();
    }

    private void init() {
        ArrayList<Integer> list = new ArrayList<>();
        Random r = new Random();
        for (int i = 1; i <= 50; i++) {
            if (((i >> n) & 1) == 1) {
                if (isFirst) {
                    list.add(i);
                } else {
                    if (r.nextBoolean()) {
                        list.addFirst(i);
                    } else {
                        list.addLast(i);
                    }
                }
            }
        }
        for (int i = 0; i < list.size(); i++) {
            cards.add(new Card(list.get(i), i));
        }
    }

    public void draw() {
        for (int i = cards.size() - 1; i >= 0; i--) {
            cards.get(i).draw();
        }
    }

    public boolean moveToFinal() {
        boolean ret = false;
        boolean signal = true;
        for (Card card : cards) {
            ret = card.moveToFinal();
            if (!ret) {
                signal = false;
            }
        }
        return ret && signal;
    }

    public boolean moveToInit() {
        boolean ret = false;
        boolean signal = true;
        for (Card card : cards) {
            ret = card.moveToInit();
            if (!ret) {
                signal = false;
            }
        }
        return ret && signal;
    }

    public boolean flipToFront() {
        boolean ret = false;
        for (Card card : cards) {
            ret = card.flipToFront();
        }
        return ret;
    }

    public boolean flipToBack() {
        boolean ret = false;
        for (Card card : cards) {
            ret = card.flipToBack();
        }
        return ret;
    }

    public void setGraphics(Graphics g) {
        for (Card card : cards) {
            card.setG2d(g);
        }
    }

    public static void initInform() {
        index = 5;
        Arrays.fill(binary, 0);
    }
    public static void calculate(boolean is1) {
        if (is1) {
            binary[index--] = 1;
        } else {
            binary[index--] = 0;
        }
    }

    public static void endCalculate() {
        StringBuilder num = new StringBuilder();
        for (int j : binary) {
            num.append(j);
        }
        HB.luckNum = Integer.parseInt(num.toString(), 2);
    }
    public void setCenter() {
        for (Card card : cards) {
            card.setCenter();
        }
    }
}
